﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using SBPweb.RIA.Client.Controls.Base;
using SBPweb.RIA.Client.Controls.Extensions;

namespace SBPweb.RIA.Client.Controls.Content
{

    public partial class ExpandableContainer : ContentControlBase
    {

        #region Constructor logic

        public ExpandableContainer()
        {
            InitializeComponent();
        }

        #endregion

        #region Dependencies

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(object), typeof(ExpandableContainer), null);
        public static readonly DependencyProperty ToggleButtonProperty = DependencyProperty.Register("ToggleButton", typeof(object), typeof(ExpandableContainer), null);
        public static readonly DependencyProperty HorizontalScrollBarVisibilityProperty = DependencyProperty.Register("HorizontalScrollBarVisibility", typeof(ScrollBarVisibility), typeof(ExpandableContainer), null);
        public static readonly DependencyProperty VerticalScrollBarVisibilityProperty = DependencyProperty.Register("VerticalScrollBarVisibility", typeof(ScrollBarVisibility), typeof(ExpandableContainer), null);
        public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(ExpandableContainer), null);

        #endregion

        #region Properties

        public bool IsExpanded
        {
            get
            {
                return GetValue<bool>(IsExpandedProperty);
            }
            set
            {
                SetValue<bool>(IsExpandedProperty, value);
            }
        }

        public object Title
        {
            get
            {
                return GetValue<object>(TitleProperty);
            }
            set
            {
                SetValue<object>(TitleProperty, value, "Title");
            }
        }

        public object ToggleButton
        {
            get
            {
                return GetValue<object>(ToggleButtonProperty);
            }
            set
            {
                SetValue<object>(ToggleButtonProperty, value, "ToggleButton");
            }
        }

        public ScrollBarVisibility HorizontalScrollBarVisibility
        {
            get
            {
                return GetValue<ScrollBarVisibility>(HorizontalScrollBarVisibilityProperty);
            }
            set
            {
                SetValue<ScrollBarVisibility>(HorizontalScrollBarVisibilityProperty, value, "HorizontalScrollBarVisibility");
            }
        }

        public ScrollBarVisibility VerticalScrollBarVisibility
        {
            get
            {
                return GetValue<ScrollBarVisibility>(VerticalScrollBarVisibilityProperty);
            }
            set
            {
                SetValue<ScrollBarVisibility>(VerticalScrollBarVisibilityProperty, value, "VerticalScrollBarVisibility");
            }
        }

        #endregion

        #region Methods

        protected override void InitializeHandlers()
        {
            base.InitializeHandlers();
            ToolBarItem tbi = ToggleButton as ToolBarItem;
            tbi.MouseLeftButtonUp += new MouseButtonEventHandler(tbiToggle_MouseLeftButtonUp);
        }

        protected override void FinalizeHandlers()
        {
            base.FinalizeHandlers();
            ToolBarItem tbiToggle = ToggleButton as ToolBarItem;
            tbiToggle.MouseLeftButtonUp -= new MouseButtonEventHandler(tbiToggle_MouseLeftButtonUp);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ToolBarItem tbi = new ToolBarItem();
            tbi.Width = 16;
            tbi.Height = 16;
            tbi.ImageSource = new BitmapImage();
            ToggleButton = tbi;
            MaintainUIElements();
        }

        #endregion

        #region Event handlers

        private void tbiToggle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsExpanded = !IsExpanded;
            MaintainUIElements();
        }

        protected virtual string GetToggleButtonImageUri()
        {
            return string.Format("/SBPweb.RIA.Client.Controls;component/Resources/{0}_16x16.png", IsExpanded ? "Minus" : "Plus");
        }

        private void MaintainUIElements()
        {
            ToolBarItem tbi = ToggleButton as ToolBarItem;
            if (tbi == null)
            {
                return;
            }
            BitmapImage bI = tbi.ImageSource as BitmapImage;
            bI.UriSource = new Uri(GetToggleButtonImageUri(), UriKind.RelativeOrAbsolute);
            VisualStateManager.GoToState(this, IsExpanded ? "Expand" : "Collapse", true);

            //Grid parent = tbi.FintParentElement<Grid>("Root");
            //if (parent == null)
            //{
            //    return;
            //}
            //ContentPresenter cpContent = parent.FindChildElement<ContentPresenter>(parent, "cpContent");
            //cpContent.Visibility = IsExpanded ? Visibility.Visible : Visibility.Collapsed;
        }

        #endregion

    }

}